package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.News;
import org.example.system.entity.OperateLog;
import org.example.system.entity.User;
import org.example.system.mapper.NewsMapper;
import org.example.system.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * 新闻服务层实现类
 */
@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    /**
     * 按条件分页查询
     * @param news
     * @return
     */
    @Override
    public IPage<News> selectPage(News news){
        MPJLambdaWrapper<News> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(News.class);
        wrapper.leftJoin(User.class,User::getId,News::getUserId);
        //条件
        if (news.getUserId() != null){
            wrapper.eq(News::getUserId,news.getUserId());
        }
        wrapper.selectAs(User::getUsername,News::getUsername);
        if (StrUtil.isNotEmpty(news.getUsername())){
            wrapper.like(User::getUsername,news.getUsername());    //相似的用户名（模糊查询）
        }
        wrapper.selectAs(User::getRealname,News::getRealname);
        if (StrUtil.isNotEmpty(news.getRealname())){
            wrapper.like(User::getRealname,news.getRealname());    //相似的真实姓名（模糊查询）
        }
        if (StrUtil.isNotEmpty(news.getTitle())){
            wrapper.like(News::getTitle,news.getTitle());    //相似的新闻标题（模糊查询）
        }
        if (StrUtil.isNotEmpty(news.getCreateBy())){
            wrapper.like(News::getCreateBy,news.getCreateBy());    //相似的发布用户名（模糊查询）
        }
        if (StrUtil.isNotEmpty(news.getUpdateBy())){
            wrapper.like(News::getUpdateBy,news.getUpdateBy());    //相似的最后编辑用户名（模糊查询）
        }
        if (news.getRecommend() != null){
            wrapper.eq(News::getRecommend,news.getRecommend()); //是否推荐
        }
        if (news.getRespond() != null){
            wrapper.eq(News::getRespond,news.getRespond()); //是否推荐
        }
        wrapper.orderByDesc(OperateLog::getId);
        IPage<News> page = new Page<>();
        page.setCurrent(news.getPageNum());
        page.setSize(news.getPageSize());
        return newsMapper.selectPage(page,wrapper);
    }

    /**
     * 添加新闻
     * @param news
     * @return
     */
    @Override
    public int add(News news) {
        news.setCreateTime(new Date());
        return newsMapper.insert(news);
    }

    /**
     * 修改
     * @param news
     * @return
     */
    @Override
    public int update(News news) {
        news.setUpdateTime(new Date());
        return newsMapper.updateById(news);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return newsMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @Override
    public News getOne(Long id) {
        News news = newsMapper.selectById(id);
        MPJLambdaWrapper<News> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(News.class);
        wrapper.leftJoin(User.class,User::getId,News::getUserId);
        wrapper.eq(News::getId,news.getId());
        wrapper.eq(News::getUserId,news.getUserId());
        wrapper.selectAs(User::getRealname,News::getRealname);
        wrapper.orderByDesc(OperateLog::getId);
        IPage<News> page = new Page<>();
        page.setCurrent(1);
        page.setSize(1);
        return newsMapper.selectPage(page,wrapper).getRecords().get(0);
    }
}
